#pragma once
#include "mysql.hpp"
#include "chat_session_member.hxx"
#include "chat_session_member-odb.hxx"

class ChatSessionMemberTable
{
    public:
    using ptr = std::shared_ptr<ChatSessionMemberTable>;

    ChatSessionMemberTable(const std::shared_ptr<odb::core::database>& db) :_db(db) {}

    ~ChatSessionMemberTable() {}

    //单个会话成员的新增
    bool append_member(ChatSessionMember& csm) {
        try
        {
            odb::transaction trans(_db->begin());
            _db->persist(csm);
            trans.commit();

        }catch(std::exception& e) {
            LOG_ERROR("新增单会话成员失败 {}-{}",csm.user_id(),e.what());
            return false;
        }
        return true;
    }

    bool append_member(std::vector<ChatSessionMember>& csm_lists) {
        try
        {
            odb::transaction trans(_db->begin());

            for(auto e : csm_lists) {
                _db->persist(e);
            }

            trans.commit();

        }catch(std::exception& e) {
            LOG_ERROR("新增多会话成员失败 {}-{}",csm_lists.size(),e.what());
            return false;
        } 

        return true;
    }


    //删除会话中指定的成员  用ssid 和uid
    bool remove_member(ChatSessionMember& csm)
    {
        try
        {
            odb::transaction trans(_db->begin());
            typedef odb::query<ChatSessionMember> query;
            _db->erase_query<ChatSessionMember>(query::session_id == csm.session_id() && query::user_id == csm.user_id());
            trans.commit();
        }catch(const std::exception& e) {
            LOG_ERROR("删除单会话失败 {}-{}",csm.user_id(),e.what());
            return false;
        }

        return true;
    }

    bool remove_member(const std::string& ssid) {
        try
        {
            odb::transaction trans(_db->begin());
            typedef odb::query<ChatSessionMember> query;
            _db->erase_query<ChatSessionMember>(query::session_id == ssid);
            trans.commit();
        }catch(const std::exception& e) {
            LOG_ERROR("删除多会话失败 {}-{}",ssid,e.what());
            return false;
        }

        return true;
    }

    std::vector<std::string> members(const std::string& ssid) {
        std::vector<std::string> res;
        try
        {
            odb::transaction trans(_db->begin());

            typedef odb::query<ChatSessionMember> query;
            typedef odb::result<ChatSessionMember> result;
            result r(_db->query<ChatSessionMember>(query::session_id == ssid));
            for(result::iterator it(r.begin()); it != r.end(); it++) {
                res.push_back(it->user_id());
            }

            trans.commit();
        }catch(const std::exception& e) {
            LOG_ERROR("获取会话中的成员失败 {}-{}",ssid,e.what());
            return res;
        }

        return res;
    }

    private:
    std::shared_ptr<odb::core::database> _db;

};
